<template>
  <view class="contenter" :style="{ 'height': setHeight + 'px' }">

    <view class="bg u-flex flex_j_c">
      <view style="position: absolute;left:60rpx;top: 100rpx" @click="black">
        <u-icon name="arrow-left"  color="#fff" size="28"></u-icon>
      </view>
      <view class="actver u-flex flex_j_c">
        <u-image :src="`/static/logo.png`"  :height="115" :width="115" :fade="false"></u-image>
      </view>
    </view>
    <view class="form">
      <view class="view u-flex">
        <u-icon name="phone"  color="#9a9c9c" size="40" ></u-icon>
        <u-input v-model="form.phone" placeholder="手机号" maxlength="11" type="text" :border="false" :height="88" />
      </view>
      <view class="view u-flex">
        <u-icon name="lock"  color="#9a9c9c" size="40"></u-icon>
        <u-input v-model="form.password" placeholder="密码" type="password" :border="false"  :height="88" />
      </view>
      <view class="bt" @click="login">登录</view>
      <view class="bt weixin u-margin-top-40" @click="login" v-if="type == 1 || type == 2">微信一键登录</view>
    </view>
  </view>
</template>

<script>
export default {
name: "login",
  data(){
    return{
      form:{
        phone:'',
        password:'',
        openid:''
      },
      type:'',// 1技师，2店长，3老板
    }
  },
  onLoad(option){
    this.type = option.type
  },
  methods:{
    black(){
      uni.navigateBack({
      	delta:1
      })
    },
    login(){

      // this.loginUser()
      //技师端第一次进来登录
      if(this.type == 1 || this.type == 2){
        const that = this
        uni.login({
          provider: 'weixin',
          success: function (data) {

            that.$api.wx_login({code:data.code}).then(res => {
              that.form.openid = res.data.openid
              //没有返回token就要密码登录
              if(!res.data.token){
                // this.$u.toast('暂未绑定哟！')
                that.loginUser()
              }else {
                if(res.data.type == 2 && that.type == 1 ){
                  return that.$u.toast('请选择门店端登录！')
                }
                if(res.data.type == 3 && that.type == 1 ){
                  return that.$u.toast('请选择老板端登录！')
                }
                if(res.data.type == 4 && that.type == 1 ){
                  return this.$u.toast('请选择区域代理端登录！')
                }

                if(res.data.type ==1 && that.type == 2 ){
                  return this.$u.toast('请选择技师端登录！')
                }
                if(res.data.type ==3 && that.type == 2 ){
                  return this.$u.toast('请选择老板端登录！')
                }
                if(res.data.type == 4 && that.type == 2 ){
                  return this.$u.toast('请选择区域代理端登录！')
                }
                uni.setStorage({
                  key: 'token',
                  data: res.data.token,
                  success: function () {
                    uni.showToast({
                      title: '登录成功',
                      icon: 'none'
                    })
                  }
                });
                uni.switchTab({
                  url:"/pages/store/index"
                })
                uni.hideHomeButton()
              }
            })
          }
        });
      }else {
        //登录
        this.loginUser()
      }
    },
    //登录
    loginUser(){
      if(!this.form.phone){
        this.$u.toast('请填写手机号！')
        return
      }
      if(!this.form.password){
        this.$u.toast('请输入密码！')
        return
      }
      this.$api.login(this.form).then(res => {

        if(res.data.type ==1 && this.type == 2 ){
          return this.$u.toast('请选择技师端登录！')
        }
        if(res.data.type ==3 && this.type == 2 ){
          return this.$u.toast('请选择老板端登录！')
        }
        if(res.data.type == 2 && this.type == 3 ){
          return this.$u.toast('请选择门店端登录！')
        }
        if(res.data.type == 2 && this.type == 4 ){
          return this.$u.toast('请选择门店端登录！')
        }
        if(res.data.type == 1 && this.type == 3 ){
          return this.$u.toast('请选择技师端登录！')
        }
        if(res.data.type == 2 && this.type == 1 ){
          return this.$u.toast('请选择门店端登录！')
        }
        if(res.data.type == 3 && this.type == 1 ){
          return this.$u.toast('请选择老板端登录！')
        }
        if(res.data.type == 4 && this.type == 1 ){
          return this.$u.toast('请选择区域代理端登录！')
        }
        if(res.data.type == 4 && this.type == 2 ){
          return this.$u.toast('请选择区域代理端登录！')
        }
        if(res.data.type == 4 && this.type == 3 ){
          return this.$u.toast('请选择区域代理端登录！')
        }
        uni.setStorage({
          key: 'token',
          data: res.data.token,
          success: function () {
            uni.showToast({
              title: '登录成功',
              icon: 'none'
            })
          }
        });

        if(res.data.type == 3 || res.data.type == 4){
          uni.switchTab({url:"/pages/index/index"})
          uni.hideHomeButton()
        }else {
          uni.switchTab({url:"/pages/store/index"})
          uni.hideHomeButton()
        }
      })
    },
  },
}
</script>

<style scoped lang="scss">
.contenter{
  background: white!important;
}

.bg{
  background: url("https://yft.xibuts.cn/upload/bg/bg4.png") no-repeat;
  background-size: 100% 100%;
  height:460rpx;
  .actver{
    width: 174rpx;
    height: 174rpx;
    background: #FFFFFF;
    border-radius: 50%;

  }

}
.form{
  width: 600rpx;
  margin:  120rpx auto;
  .view{
    width: 600rpx;
    position: relative;
    margin-bottom: 36rpx;
    height: 88rpx;
    border: 2rpx solid #E3E3E3;
    border-radius: 64rpx;
  }
  /deep/.u-input{
    padding-left: 20rpx !important;
    width: 500rpx;
  }/deep/.u-icon{
    padding-left: 30rpx !important;
  }
}
</style>